﻿using System;

namespace Qplus.Infrastructure
{
    public class AsyncResult<TResult> : AsyncResultNoResult
    {
        private TResult m_result;//异步操作完成返回的对象

        public AsyncResult(AsyncCallback asyncCallback, object state)
            : base(asyncCallback, state)
        {
            this.m_result = default(TResult);
        }
        /// <summary>
        /// 停止异步返回返回对象
        /// </summary>
        /// <returns></returns>
        public TResult EndInvoke()
        {
            base.EndInvoke(); //等待操作完成 
            return m_result;  // 返回结果
        }
        /// <summary>
        /// 操作完成
        /// </summary>
        /// <param name="result">返回结果对象</param>
        /// <param name="completedSynchronously">是否同步操作</param>
        public void SetAsCompleted(TResult result, bool completedSynchronously)
        {
            //保存异步操作的结果
            m_result = result;
            base.SetAsCompleted(null, completedSynchronously);
        }
    }
}
